package hhm.android.account

import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import com.alibaba.android.arouter.launcher.ARouter
import com.igexin.sdk.PushManager
import com.trello.rxlifecycle4.RxLifecycle
import com.trello.rxlifecycle4.android.ActivityEvent
import hhm.android.account.databinding.ActivityLibraryThreeBinding
import hhm.android.library.utils.DataStoreUtils
import hhm.android.library.utils.DialogUtils
import hhm.android.library.utils.GetStringListener
import hhm.android.library.utils.OnMultiClickListener
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.async
import siau.android.base.HttpHelper
import siau.android.base.MAIN_ROUTER
import siau.android.base.SBApplication
import siau.android.base.SBBaseActivity
import siau.android.http.model.CreateFamilyRequest
import siau.android.library.KeyString

class CreateLibraryThreeActivity : SBBaseActivity(), CoroutineScope by MainScope() {

    lateinit var dataBinding: ActivityLibraryThreeBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleWhiteAndTextBlank()
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_library_three)
        initTitle()
        dataBinding.lifecycleOwner = this

        dataBinding.activityLibraryThreeSelectHeight.setOnClickListener {
            DialogUtils.showHeightPicBottomView(this, "CM", object : GetStringListener {
                override fun getString(s: String) {
                    dataBinding.activityLibraryThreeSelectHeight.text = s
                }
            })
        }
        dataBinding.activityLibraryThreeSelectWeight.setOnClickListener {
            DialogUtils.showWeightPicBottomView(this, "KG", object : GetStringListener {
                override fun getString(s: String) {
                    dataBinding.activityLibraryThreeSelectWeight.text = s
                }
            })
        }

        dataBinding.activityLibraryThreeBtn.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                showLoadingDialog()
                val identityType = intent.getIntExtra(KeyString.identityType, 0)
                val userIdentity = intent.getStringExtra(KeyString.userIdentity)!!
                val nickName = intent.getStringExtra(KeyString.nickName)!!
                val birthday = intent.getLongExtra(KeyString.birthday, 0)
                val sex = intent.getIntExtra(KeyString.sex, 0)
                val avatar = intent.getStringExtra(KeyString.avatar)
                val height = dataBinding.activityLibraryThreeSelectHeight.text.toString().replace(getString(R.string.class_cm), "")
                val weight = dataBinding.activityLibraryThreeSelectWeight.text.toString().replace(getString(R.string.class_kg), "")
                val request = CreateFamilyRequest(identityType, userIdentity, nickName, birthday, sex, height, weight, avatar ?: "", "CM", "KG")
                HttpHelper().createFamily(request)?.compose(RxLifecycle.bindUntilEvent(lifecycle(), ActivityEvent.STOP))?.subscribe({
                    HttpHelper().getDefaultFamily()?.compose(RxLifecycle.bindUntilEvent(lifecycle(), ActivityEvent.STOP))?.subscribe({
                        hideLoadingDialog()
                        SBApplication.userData = it
                        SBApplication.hasWeightMachine = false
                        SBApplication.hasHeightMachine = false
                        SBApplication.hasToothBrushMachine = false
                        it.deviceList?.forEach { d ->
                            if (d.deviceType == 1) {
                                SBApplication.hasWeightMachine = true
                            } else if (d.deviceType == 2) {
                                SBApplication.hasHeightMachine = true
                            } else if (d.deviceType == 3) {
                                SBApplication.hasToothBrushMachine = true
                            }
                        }

                        PushManager.getInstance().bindAlias(this@CreateLibraryThreeActivity, SBApplication.userData.userAlias)
                        async {
                            DataStoreUtils.writeStringToDataStore(KeyString.userAlias, SBApplication.userData.userAlias)
                        }

                        ARouter.getInstance().build(MAIN_ROUTER).navigation()
                        setResult(1024)
                        finish()
                    }, {
                        hideLoadingDialog()
                        showToast(it.message)
                    })
                }, {
                    hideLoadingDialog()
                    showToast(it.message)
                })
            }
        })
    }
}